<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/11
 * Time: 16:50
 */

namespace HiCommon\Service;

use RongCloud\Api as RCloud;

class RongService extends BaseService
{

    public function config_set( string $key = null ){
        //生成环境   $appKey = 4z3hlwrv45zlt    $appSecret = yNQdK0sH24
        //开发环境   $appKey = pwe86ga5p9wp6    $appSecret = FO2Na2Xs9bw

        //开发环境
        $data = [
            'app_key' => 'pwe86ga5p9wp6',
            'app_secret' => 'FO2Na2Xs9bw'
        ];

        //生产环境
//        $data = [
//          'app_key' => '4z3hlwrv45zlt',
//          'app_secret' => 'yNQdK0sH24'
//        ];
        if( empty( $key ) ){
            return $data;
        }else{
            return $data[$key];
        }
    }

    /**
     *  获取用户的Token
     *  hinq 2019 12 10
     *
     **/
    public function getToken( array $user ){
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->getToken( $user['user_id'], $user['name'], $user['portrait_uri'] );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        //  依赖第三方库
        if( $res['code'] == 200 ){
            return hello_success('ok',$res);
        }
        return hello_error('Token获取失败');
    }

    /**
     *  更新用户的信息
     *  hinq 2019 12 10
     *
     **/
    public function userRefresh( array $user ){
//        $user = api_user();
//        $user_id = $user['username'];
//        $name = $user['nickname'];
//        $portrait_uri = oss_full_url( $user['head_img'] );
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->userRefresh( $user['user_id'], $user['name'], $user['portrait_uri'] );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        if( $res['code'] == 200 ){
            return hello_success('ok');
        }
        return hello_error('用户信息修改成功');
    }


    /**
     *  用户-》用户 发送消息
     *  hinq 2019 12 10
     *
     **/
    public function messagePush(  $fromUserId , $toUserId ,$objectName , $content){
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->messagePrivatePublish($fromUserId,$toUserId,$objectName, $content );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        if( $res['code'] == 200 ){
            return hello_success($res);
        }
        return hello_error('消息发送失败');
    }


    /**
     *  检测指定用户是否在线
     *  hinq 2019 12 10
     *
     **/
    public function online( $userId ){
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->userCheckOnline( $userId );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        if( $res['code'] == 200 ){
            $res['status'] = $res['status'] == 1 ? '在线' : '离线';
            return hello_success( 'ok',  $res );
        }
        return hello_error('获取失败');
    }


    /**
     *  获取 APP 内指定某天某小时内的所有会话消息记录的下载地址
     *  hinq 2020 4 23
     *
     **/
    public function messageHistory( $data ){
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->messageHistory( $data['date'] );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        if( $res['code'] == 200 ){
            return hello_success($res);
        }
        return hello_error('获取失败');
    }


    public function push( $arr_input ){
        $api = new RCloud( $appKey = self::config_set('app_key') , $appSecret=self::config_set('app_secret'));
        $res = $api->push( $arr_input['platform'] , $arr_input['audience'] , $arr_input['notification'] );
        if( isset( $res['state'] ) &&  $res['state'] == 1 ){
            return $res;
        }
        $res = json_decode( $res ,  true );
        dd( $res);
        if( $res['code'] == 200 ){
            return hello_success($res);
        }
        return hello_error('获取失败');

    }



}